﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Ivi.ConfigServer.Interop;
using AIRO.Devices.Solenoid;

namespace AIRO.BFSerial
{
    public partial class fmHardwareConfig : Form
    {
        fmMain _fmMain;
        Ivi.ConfigServer.Interop.IviConfigStore cs;
        public fmHardwareConfig()
        {
            InitializeComponent();
        }
        public fmHardwareConfig(fmMain fm): this()
        {
            _fmMain = fm;

            cs = new IviConfigStoreClass();
            string location;
            if (cs.ProcessDefaultLocation != "")
            {
                location = cs.ProcessDefaultLocation;
            }
            else if (cs.MasterLocation != "")
            {
                location = cs.MasterLocation;
            }
            else
            {
                throw new Exception("Process Default Locaqtion and Master Location are null strings!");
            }
            cs.Deserialize(location);            
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            //_fmMain.rfGeneratorLogicalName = cbxRFGeneratorLogicalName.Text;

            AIRO.BFSerial.Properties.Settings.Default.RFGeneratorLogicalName = cbxRFGeneratorLogicalName.Text;
            AIRO.BFSerial.Properties.Settings.Default.VoltmeterLogicalName = cbxVoltmeterLogicalName.Text;
            AIRO.BFSerial.Properties.Settings.Default.PSLogicalName = cbxPSLogicalName.Text;
            AIRO.BFSerial.Properties.Settings.Default.TempSensorLogicalName = cbxTempSensorLogicalName.Text;

            this.Hide();
            _fmMain.Activate();

        }

        private void fmHardwareConfig_FormClosing(object sender, FormClosingEventArgs e)
        {

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Hide();
            _fmMain.Activate();
        }

        private void fmHardwareConfig_Load(object sender, EventArgs e)
        {
            string defaultRFGenerator = AIRO.BFSerial.Properties.Settings.Default.RFGeneratorLogicalName;
            string defaultVoltmeter = AIRO.BFSerial.Properties.Settings.Default.VoltmeterLogicalName;
            string defaultPS = AIRO.BFSerial.Properties.Settings.Default.PSLogicalName;
            string defaultTempSensor = AIRO.BFSerial.Properties.Settings.Default.TempSensorLogicalName;

            cbxRFGeneratorLogicalName.Text = "";
            cbxRFGeneratorLogicalName.Items.Clear();
            cbxVoltmeterLogicalName.Text = "";
            cbxVoltmeterLogicalName.Items.Clear();
            cbxPSLogicalName.Text = "";
            cbxPSLogicalName.Items.Clear();
            cbxTempSensorLogicalName.Text = "";
            cbxTempSensorLogicalName.Items.Clear();

            foreach (IviLogicalName logicalName in cs.LogicalNames)
            {
                if (logicalName.Session != null)
                {
                    IviSoftwareModule softwareModule = logicalName.Session.SoftwareModule;
                    if (softwareModule != null)
                    {
                        IviPublishedAPICollection publishedAPIs = softwareModule.PublishedAPIs;


                        foreach (IviPublishedAPI publishedAPI in publishedAPIs)
                        {
                            if (publishedAPI.Type == "IVI-COM")
                            {
                                if (publishedAPI.Name == "IviRFSigGen")
                                {
                                    cbxRFGeneratorLogicalName.Items.Add(logicalName.Name);
                                    if (logicalName.Name == defaultRFGenerator)
                                        cbxRFGeneratorLogicalName.SelectedItem = defaultRFGenerator;
                                }
                                if (publishedAPI.Name == "IviDmm")
                                {
                                    cbxVoltmeterLogicalName.Items.Add(logicalName.Name);
                                    if (logicalName.Name == defaultVoltmeter)
                                        cbxVoltmeterLogicalName.SelectedItem = defaultVoltmeter;

                                    cbxTempSensorLogicalName.Items.Add(logicalName.Name);
                                    if (logicalName.Name == defaultTempSensor)
                                        cbxTempSensorLogicalName.SelectedItem = defaultTempSensor;
                                }


                            }
                        }
                        string progID = softwareModule.ProgID;
                        Type type = Type.GetTypeFromProgID(progID);
                        if (type != null)
                        {
                            foreach (Type tInterface in type.GetInterfaces())
                            {
                                // Является ли этот интерфейс ISolenoid, т.е. тем что мы ищем
                                if (tInterface == typeof(ISolenoid))
                                {
                                    //Если да, то добавляем logicalName в список опций
                                    cbxPSLogicalName.Items.Add(logicalName.Name);
                                    if (logicalName.Name == defaultPS)
                                        cbxPSLogicalName.SelectedItem = defaultPS;
                                }
                            }
                        }
                    }
                }
            }
            if (cbxVoltmeterLogicalName.Items.Count > 0)
            {
                /*
                if (cbxVoltmeterLogicalName.Text == "")
                    cbxVoltmeterLogicalName.SelectedIndex = 0;
                 * */
            }
            else
            {
                gbVoltmeter.Enabled = false;
            }

            if (cbxRFGeneratorLogicalName.Items.Count > 0)
            {
                /*
                if (cbxRFGeneratorLogicalName.Text == "")
                    cbxRFGeneratorLogicalName.SelectedIndex = 0;
                 * */
            }
            else
            {
                gbRFGenerator.Enabled = false;
                MessageBox.Show("Error! No IVI RF Signal Generator found", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            if (cbxPSLogicalName.Items.Count > 0)
            {
            }
            else
            {
                gbPowerSupply.Enabled = false;
            }
            if (cbxTempSensorLogicalName.Items.Count > 0)
            {
            }
            else
            {
                gbTempSensor.Enabled = false;
            }
        }

    }
}
